package dao;

import bean.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 骑行用户之间的私信、activity和advice的管理员反馈信息的统一格式
 * 所有信息的状态为-1为骑行用户未读，1为骑行用户已读
 *
 * @author 邓晴予
 */
public class AllMessageDao {
    private UserDao userDao = new UserDao();

    /**
     * 将Message列表的信息提取出来
     *
     * @param messages the messages
     * @return list
     */
    public List<AllMessage> parseFromMessages(List<Object> messages) {
        List<AllMessage> allMessages = new ArrayList<>();
        for (Object message1 : messages) {
            try {
                Message message = (Message) message1;
                AllMessage allMessage = new AllMessage(message.getMessageId(), userDao.getUserNameByUserId(message.getSendId()), message.getContent(), message.getSendTime(), message.getStatus());
                allMessages.add(allMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return allMessages;
    }

    /**
     * 将Message列表的信息提取出来
     *
     * @param message the message
     * @return all message
     */
    public AllMessage parseFromMessage(Message message) {
        try {
            return new AllMessage(message.getMessageId(), userDao.getUserNameByUserId(message.getSendId()), message.getContent(), message.getSendTime(), message.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Parse from activities list.
     *
     * @param activities the activities
     * @return the list
     */
    public List<AllMessage> parseFromActivities(List<Object> activities) {
        List<AllMessage> allMessages = new ArrayList<AllMessage>();
        for (int i = 0; i < activities.size(); ++i) {
            try {
                Activity activity = (Activity) activities.get(i);
                String status = null;
                String activityStatus = activity.getStatus();
                if ("2".equals(activityStatus) || "3".equals(activityStatus)) {
//					骑行用户未读管理员对该活动的回复信息
                    status = "-1";
                } else if ("4".equals(activityStatus) || "5".equals(activityStatus)) {
//					骑行用户已读管理员对该活动的回复信息
                    status = "1";
                } else {
//					管理员未读骑行用户发送的活动请求／管理员已读但没回复
                    break;
                }
                AllMessage allMessage = new AllMessage(activity.getActivityId(), "管理员", activity.getReplyContent(), activity.getReplyTime(), status);
                allMessages.add(allMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return allMessages;
    }

    /**
     * Parse from activity all message.
     *
     * @param activity the activity
     * @return the all message
     */
    public AllMessage parseFromActivity(Activity activity) {
        String status = null;
        String activityStatus = activity.getStatus();
        if ("2".equals(activityStatus) || "3".equals(activityStatus)) {
//			骑行用户未读管理员对该活动的回复信息
            status = "-1";
        } else if ("4".equals(activityStatus) || "5".equals(activityStatus)) {
//			骑行用户已读管理员对该活动的回复信息
            status = "1";
        } else {
//			管理员未读骑行用户发送的活动请求／管理员已读但没回复
            return null;
        }
        return new AllMessage(activity.getActivityId(), "管理员", activity.getReplyContent(), activity.getReplyTime(), status);
    }

    /**
     * Parse from advices list.
     *
     * @param advices the advices
     * @return the list
     */
    public List<AllMessage> parseFromAdvices(List<Object> advices) {
        List<AllMessage> allMessages = new ArrayList<AllMessage>();
        for (int i = 0; i < advices.size(); ++i) {
            try {
                Advice advice = (Advice) advices.get(i);
                String status = null;
                String adviceStatus = advice.getStatus();
                if ("2".equals(adviceStatus)) {
//					骑行用户未读管理员对advice给出对回复
                    status = "-1";
                } else if ("3".equals(adviceStatus)) {
//					骑行用户已读管理员对advice给出的回复
                    status = "1";
                } else {
//					管理员未读advice／管理员已读advice但未回复
                    break;
                }
                AllMessage allMessage = new AllMessage(advice.getAdviseId(), "管理员", advice.getReplyContent(), advice.getReplyTime(), status);
                allMessages.add(allMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return allMessages;
    }

    /**
     * Parse from advice all message.
     *
     * @param advice the advice
     * @return the all message
     */
    public AllMessage parseFromAdvice(Advice advice) {
        String status = null;
        String adviceStatus = advice.getStatus();
        if ("2".equals(adviceStatus)) {
//			骑行用户未读管理员对advice给出对回复
            status = "-1";
        } else if ("3".equals(adviceStatus)) {
//			骑行用户已读管理员对advice给出的回复
            status = "1";
        } else {
//			管理员未读advice／管理员已读advice但未回复
            return null;
        }
        return new AllMessage(advice.getAdviseId(), "管理员", advice.getReplyContent(), advice.getReplyTime(), status);
    }

    /**
     * Parse list.
     *
     * @param messages   the messages
     * @param activities the activities
     * @param advices    the advices
     * @return the list
     */
    public List<AllMessage> parse(List<Object> messages, List<Object> activities, List<Object> advices) {
        List<AllMessage> allMessages = new ArrayList<AllMessage>();
        if (messages != null && messages.size() != 0) {
            allMessages.addAll(this.parseFromMessages(messages));
        }
        if (activities != null && activities.size() != 0) {
            allMessages.addAll(this.parseFromActivities(activities));
        }
        if (advices != null && advices.size() != 0) {
            allMessages.addAll(this.parseFromAdvices(advices));
        }
        return allMessages;
    }

    /**
     * Gets page.
     *
     * @param allMessages the all messages
     * @param pageBean    the page bean
     * @return page
     */
    public List<AllMessage> getPage(List<AllMessage> allMessages, PageBean pageBean) {
        List<AllMessage> resultAllMessages = new ArrayList<AllMessage>();
        int end = ((pageBean.getStart() + pageBean.getRows()) < allMessages.size()) ? (pageBean.getStart() + pageBean.getRows()) : allMessages.size();
        for (int i = pageBean.getStart(); i < end; ++i) {
            resultAllMessages.add(allMessages.get(i));
        }
        return resultAllMessages;
    }
}
